package com.zzptc.sky.baidudemo.activities;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.zzptc.sky.baidudemo.utils.MyApplication;
import com.zzptc.sky.baidudemo.R;
import com.zzptc.sky.baidudemo.base.BaseActivity;
import com.zzptc.sky.baidudemo.services.IVersionUpdate;
import com.zzptc.sky.baidudemo.vo.VersionInfo;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class CheckUpdateActivity extends BaseActivity {
    private ImageView iv_one;
    private ImageView iv_two;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_check_update);

        iv_one = (ImageView) findViewById(R.id.iv_one);
        iv_two = (ImageView) findViewById(R.id.iv_two);
        iv_two.setVisibility(View.INVISIBLE);
        ObjectAnimator oaOneY = ObjectAnimator.ofFloat(iv_one,"scaleY",0f,1f,0f);

        ObjectAnimator oaOneX = ObjectAnimator.ofFloat(iv_one,"scaleX",0f,1f,0f);

        final AnimatorSet asOne = new AnimatorSet();
        asOne.play(oaOneX).with(oaOneY);
        asOne.setDuration(5000);
        asOne.start();

        ObjectAnimator oaTwoY = ObjectAnimator.ofFloat(iv_two,"scaleY",0f,1f,0f);

        ObjectAnimator oaTwoX = ObjectAnimator.ofFloat(iv_two,"scaleX",0f,1f,0f);

        final AnimatorSet asTwo = new AnimatorSet();
        asTwo.play(oaTwoX).with(oaTwoY);
        asTwo.setDuration(5000);

        asOne.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                iv_two.setVisibility(View.VISIBLE);
                asTwo.start();
            }
        });

        asTwo.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                asOne.start();
            }
        });

        if (checkNet()){
            Retrofit retrofit = new Retrofit.Builder().baseUrl("http://192.168.1.137:8080/").addConverterFactory(GsonConverterFactory.create()).build();
            IVersionUpdate iVersionUpdate = retrofit.create(IVersionUpdate.class);
            Call<VersionInfo> versionInfoCall = iVersionUpdate.getVersion();
            versionInfoCall.enqueue(new Callback<VersionInfo>() {
                @Override
                public void onResponse(Call<VersionInfo> call, Response<VersionInfo> response) {
                    //网络正常

                    VersionInfo versionInfo = response.body();
                    if (versionInfo == null){
                        Toast.makeText(CheckUpdateActivity.this, "versionInfo为空", Toast.LENGTH_SHORT).show();
                        Log.d("111","versionInfo为空");
                        return;
                    }
                    int netcode = versionInfo.getVersioncode();//最新的版本号
                    int oldcode = getVersionCode(MyApplication.getContext());
                    if (netcode>oldcode){
                        Intent intent = new Intent();
                        intent.putExtra("update",response.body());
                        setResult(3,intent);
                        finish();
                    }else {
                        setResult(4);
                        finish();
                    }
                }

                @Override
                public void onFailure(Call<VersionInfo> call, Throwable t) {
                    //网络不正常
                    setResult(2);
                    finish();
                }
            });
        }else {
            //没有网络
            setResult(1);
            finish();
        }
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_check_update;
    }

    public boolean checkNet(){

        NetworkInfo.State wifiState = null;
        NetworkInfo.State mobileState = null;

        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();

        mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        if (wifiState != null && mobileState != null
                && NetworkInfo.State.CONNECTED != wifiState
                && NetworkInfo.State.CONNECTED == mobileState) {
            // 手机网络连接成功
            return true;
        } else if (wifiState != null && mobileState != null
                && NetworkInfo.State.CONNECTED != wifiState
                && NetworkInfo.State.CONNECTED != mobileState) {
            // 手机没有任何的网络
            return false;
        } else if (wifiState != null
                && NetworkInfo.State.CONNECTED == wifiState) {
                // 无线网络连接成功
            return true;
        }


        return  false;
    }

    public static  int getVersionCode(Context context){
        try {
            PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(),0);
            return pi.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return 0;
        }
    }
}
